package com.lunduhotel.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lunduhotel.exceptions.ExceptionForeignKey;
import com.lunduhotel.factory.ConnectionFactory;
import com.lunduhotel.modelo.Cliente;
import com.lunduhotel.modelo.Quarto;

public class ClienteDao {

	private Connection con;
	
	public ClienteDao(){
		ConnectionFactory factory = new ConnectionFactory();
		con = factory.getConnection();
	}
		
	public void adicionaCliente(Cliente cliente){
		try {
		
			String sql = "insert into cliente(nome_cliente,idade,cpf,telefone,email) values (?,?,?,?,?);";
			PreparedStatement stmt;
				stmt = con.prepareStatement(sql);
			
			stmt.setString(1, cliente.getNome());
			stmt.setInt(2, cliente.getIdade());
			stmt.setString(3, cliente.getCpf());
			stmt.setString(4, cliente.getTelefone());
			stmt.setString(5, cliente.getEmail());
		
			stmt.execute();

				
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		} 
	
	}
		
	public void atualizarCliente(Cliente cliente){
			try {
				
				String sql = "update cliente set nome_cliente=?,idade=?,cpf=?,telefone=?,email=?;";
					PreparedStatement stmt;
					stmt = con.prepareStatement(sql);
					
					stmt.setString(1, cliente.getNome());
					stmt.setInt(2, cliente.getIdade());
					stmt.setString(3, cliente.getCpf());
					stmt.setString(4, cliente.getTelefone());
					stmt.setString(5, cliente.getEmail());
					
					stmt.execute();
				
				
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
		
		public void removerCliente(int id){
			try {
				
				String sql = "delete from cliente where id_cliente=?;";
					PreparedStatement stmt;
					stmt = con.prepareStatement(sql);
					stmt.setInt(1, id);
					
					stmt.execute();
				
				
			} catch (SQLException e) {
				e.printStackTrace();
				if(e.getMessage().contains("violates foreign key")){
					throw new ExceptionForeignKey("O cliente possui reservas");
				}else{
					throw new RuntimeException();
				}
			}
		}
		
		public Cliente getCliente(String cpf){
			
			Cliente cliente = new Cliente();
			String sql = "select id_cliente,nome_cliente,idade,cpf,telefone,email from cliente where cpf = ?";
			
			try {
				PreparedStatement stmt = con.prepareStatement(sql);
				stmt.setString(1, cpf);
				ResultSet rs = stmt.executeQuery();
				
				if(rs.next()){
					cliente.setId(rs.getInt(1));
					cliente.setNome(rs.getString(2));
					cliente.setIdade(rs.getInt(3));
					cliente.setCpf(rs.getString(4));
					cliente.setTelefone(rs.getString(5));
					cliente.setEmail(rs.getString(6));
					return cliente;
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
			
			
			return null;
		}
		

		 public List<Cliente> getLista(){
			   List<Cliente> list = new ArrayList<Cliente>();
			   
			   String sql = "select id_cliente,nome_cliente,idade,cpf,telefone, email from cliente;";
			   
			   try {
				ResultSet rs = con.prepareStatement(sql).executeQuery();
				while(rs.next()){
						Cliente cliente = new Cliente();
						cliente.setId(rs.getInt(1));
						cliente.setNome(rs.getString(2));
						cliente.setIdade(rs.getInt(3));
						cliente.setCpf(rs.getString(4));
						cliente.setTelefone(rs.getString(5));
						cliente.setEmail(rs.getString(6));
						
						
						list.add(cliente);
						
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
			   
			   return list;
		   }
		
	}
	
	